package com.example.demo.controller;

import com.example.demo.pojo.Result;
import com.example.demo.pojo.login.LoginDTO;
import com.example.demo.pojo.login.LoginRequest;
import com.example.demo.service.LoginService;
import com.example.demo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@CrossOrigin
@RestController
public class LoginContorl {
    @Autowired
    LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request){
        LoginDTO login = loginService.login(request);
        if(login!=null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("username", login.getUsername());
            claims.put("email", login.getEmail());
            claims.put("phone",login.getPhone());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误，loginContorl");
    }
}
